package com.qidian.buy.cart.controller;

import com.qidian.buy.cart.service.ICartService;
import com.qidian.buy.entity.User;
import com.qidian.buy.util.pojo.BuyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author 戴着假发的程序员
 * @Company 起点编程
 * @date 2023/5/26 15:14
 */
@RestController
@RequestMapping("/buy/cart")
@CrossOrigin
public class CartController {
    @Autowired
    private ICartService cartService;

    @RequestMapping("/addCart")
    public BuyResult addCart(HttpServletRequest request,Long itemId,int count){
        // 从request中取出用户信息
        User user = (User) request.getAttribute("user");
        return cartService.addCart(user,itemId,count);
    };

    @RequestMapping("/updateCart")
    public BuyResult updateCart(HttpServletRequest request,Long itemId,int count){
        // 从request中取出用户信息
        User user = (User) request.getAttribute("user");
        return cartService.updateCart(user,itemId,count);
    };

    @RequestMapping("/deleteCart")
    public BuyResult deleteCart(HttpServletRequest request,Long itemId){
        // 从request中取出用户信息
        User user = (User) request.getAttribute("user");
        return cartService.deleteCart(user,itemId);
    };

    @RequestMapping("/list")
    public BuyResult deleteCart(HttpServletRequest request){
        // 从request中取出用户信息
        User user = (User) request.getAttribute("user");
        return cartService.list(user);
    };
}
